/**
 * Created by sxwu on 14-2-15.
 */


var log = require("../lib/log");
var config = require('../config');
var customerinfo = require('../models/customerinfo');
var customerlog = require('../models/customerlog');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');
//var domain = require('domain');


var status="error";

exports.index = function(request, response){
    request.session.lastpage="querycustlog.html";
    if(request.session.admin==undefined)
    {
        response.redirect("login.html");
    }
    else
    {

        var method = request.method || '';
        var data = RUtil.ap_data(request);
        data.front_data.title = '终端客户管理';
        data.front_data.subtitle='客户日志查询';
        data.front_data.usernick =request.session.admin.usernick;
        data.front_data.usertype =request.session.admin.usertype;


        if(method.toUpperCase() === "GET") {
            //注销操作
            if(request.query.oper == "del")
            {
                customerinfo.updateInactive(request.query.id, function(error, results) {
                    if((error)!== null)
                    {
                        // 数据库异常
                        data.front_data.errDesp = "下线失败：系统内部错误(错误码："+error.errno+")";
                        data.front_data.status="danger";
                    }
                    else if(results.length <= 0) {
                        // 不存在，直接响应页面
                        data.front_data.errDesp = "下线失败：系统内部错误(错误码："+error.errno+")";
                        data.front_data.status="danger";
                    } else {
                        //存在数据将数据发到页面
                        data.front_data.errDesp = "注销成功！";
                        data.front_data.status="success";
                    }
                });
            }

            customerlog.selectForLog(request.session.admin.userid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    data.front_data.errDesp = "查询失败：系统内部错误(错误码："+error.errno+")";
                    data.front_data.status="danger";
                    response.render("querycustlog", data);
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    data.front_data.errDesp = "现在还没有客户，别急，喝杯茶就有客户来了";
                    data.front_data.status="success";
                    response.render("querycustlog", data);
                } else {
                    //存在数据将数据发到页面

                    data.info=JSON.stringify(results);
                    response.render("querycustlog", data);
                }
            });

        }

        else if(method.toUpperCase() === "POST") {
            //post需要区分不同的oper类型
        }
    }
};
